#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Inserts server.trace into <div id="trace/>

The method trace(str) of the server object can be
used anytime to log messages. Normally, they will only
be shown when an internal server error occurs. However,
with this filter it's possible to insert them into
<div id="trace"> anytime.
Useful for debugging.
"""

import xml.dom.minidom
import time

def execute_minidom(server, document, parameters):
	for n in document.getElementsByTagName('div'):
		if n.hasAttribute('id') and n.getAttribute('id') == 'trace':
			pre = document.createElement('pre')
			for trace in server.get_trace():
				log_time = '%s:%s:%s ' % trace.time[3:6]
				pre.appendChild(document.createTextNode(log_time))
				pre.appendChild(document.createTextNode(trace.message + '\n'))
				n.appendChild(pre)
